package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.Permission;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.pojo.User;
import com.wty.cmims.service.PermissionService;
import com.wty.cmims.mapper.PermissionMapper;
import com.wty.cmims.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【permission】的数据库操作Service实现
* @createDate 2024-12-05 08:56:47
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    @Lazy
    private UserService userService;
    @Override
    public ResponseResult<Map<String,Object>> findPermission(Integer id) {
        ResponseResult<Map<String,Object>> responseResult = new ResponseResult<>();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId,id);
        User user = userService.getOne(wrapper);
        Integer role;
        if (user != null) {
            role = user.getRole();
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("用户不存在");
            return responseResult;
        }
        Map<String,Object> map = new HashMap<>();
        if (role == 1){
            map = permissionMapper.findDoctorPermission(id);
        } else if (role == 2){
            map = permissionMapper.findAdminPermission(id);
        }
        if (!map.isEmpty()){
            // 将map中的键值对放入list
            List<Map<String,Object>> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                int value;
                if ((Boolean) entry.getValue()){
                    value = 1;
                } else {
                    value = 0;
                }
                Map<String,Object> mapTemp = new HashMap<>();
                mapTemp.put("name",key);
                mapTemp.put("value",value);
                list.add(mapTemp);
            }
            responseResult.setDatas(list);
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("权限信息查询成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("权限信息查询失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> updatePermission(Map<String, Object> paramsMap) {
        // user_id name value
        ResponseResult<Void> responseResult = new ResponseResult<>();
        int value = Integer.parseInt(paramsMap.get("value").toString());
//        if (value == 0){
//            paramsMap.put(paramsMap.get("name").toString(),1);
//        } else {
//            paramsMap.put(paramsMap.get("name").toString(),0);
//        }
        paramsMap.put(paramsMap.get("name").toString(),value);
        Integer rows = permissionMapper.updatePermission(paramsMap);
        if (rows > 0) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("权限更新成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("权限更新失败");
        }
        return responseResult;
    }
}




